﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ElfTyping.States.GameState.Effects;
using ElfTyping.Animations;
using ElfTyping.General;
using Microsoft.Xna.Framework;
using ElfTyping.States.GameState.Entities;

namespace ElfTyping.States.GameState.PlayerSpells
{
    public class ElfireSpell : PlayerSpell
    {
        public ElfireSpell()
        {
            Name = "Elfire";
            Description = "Canas deals massive\ndamage to an enemy.";
            ShotCoolDown = 20;
            DeactivationTime = 4;
            WordList = new string[] { "ABLAZE", "AFLAME", "BLAZED", "BURNED", "BRANDS", "BROILS", "COOKED", "KINDLE", "FLARES", "HEATED", "IGNITE", "MELTED", "ROASTS", "SCORCH", "SEARED", "SINGED", "TOASTY" };
            Effect = new ElfireEffect(this);
            IndicatorAnimation = new FireEmblemNormalStanceAnimation(@"Textures\GameState\ElfireMage");
            CardImage = new Animation(@"Textures\Cards\Spell_Elfire");
        }

        private class ElfireEffect : GameEffect
        {
            const float StartTimeOfCast = 1f;
            const float StartTimeOfExplode = 2f;
            const float StopTimeOfText = 4f;

            const int Damage = 20;

            TextBox TextBox = new TextBox("Elfire!");

            Animation CasterAnimation = new FireEmblemFocusStanceAnimation(@"Textures\GameState\ElfireMage");
            Animation BallAnimation = new ElfireBallAnimation();
            Animation ExploideAnimation = new ElfireExplodeAnimation();

            bool shouldInitializeBoxes = true;
            Rectangle CasterBox;
            Rectangle CasterSpellBox;
            //Rectangle ExplodeBox;

            ElfireSpell Spell;
            Enemy targetEnemy = null;

            public ElfireEffect(ElfireSpell spell)
                : base()
            {
                Spell = spell;
                TimeLeft = StopTimeOfText;
                CurrentAnimation = Animation.EmptyAnimation;
                DrawBox = Rectangle.Empty;

                TextBox.Portrait = new Animation(@"Textures\Characters\Canas_SmallPortrait");
                TextBox.IsPortraitOnLeft = false;
            }

            void InitializeBoxes()
            {
                CasterBox = Script.GetPlayerDrawBox();
                CasterBox.Offset(CasterBox.Width * 3 / 2, 0);

                CasterSpellBox = CasterBox;
                CasterSpellBox.Height = CasterBox.Width * 48 / 104;
                CasterSpellBox.Offset(0, CasterBox.Width * 56 / 104);

                shouldInitializeBoxes = false;
            }

            public override void OnEnd()
            {
                Spell.Effect = new ElfireEffect(Spell);
            }

            public override void Tick(float dt)
            {
                if (shouldInitializeBoxes)
                {
                    InitializeBoxes();
                }

                CasterAnimation.Advance(dt);

                if (TimeElapsed <= StartTimeOfCast && StartTimeOfCast <= TimeElapsed + dt)
                { // Start Casting
                    CurrentAnimation = BallAnimation;
                    DrawBox = CasterSpellBox;
                }
                else if (TimeElapsed <= StartTimeOfExplode && StartTimeOfExplode <= TimeElapsed + dt)
                { // Explode!
                    List<Enemy> enemies = Script.GetEnemies();
                    if (enemies.Count > 0)
                    {
                        foreach (Enemy enemy in enemies)
                        {
                            if (targetEnemy == null || enemy.CenterPosition.X > targetEnemy.CenterPosition.X && enemy.IsActive)
                            {
                                targetEnemy = enemy;
                            }
                        }

                        targetEnemy.TakeDamage(Damage);
                    }
                }

                if (targetEnemy != null)
                {
                    DrawBox = targetEnemy.DrawBox;
                    CurrentAnimation = ExploideAnimation;

                }

                if (TimeElapsed <= StopTimeOfText)
                {
                    TextBox.Tick(dt);
                }

                base.Tick(dt);
            }

            public override void Draw(General.Camera camera)
            {
                if (TimeElapsed < StartTimeOfExplode)
                {
                    CasterAnimation.FlippedHorizontally = false;
                    CasterAnimation.Draw(camera, CasterBox);
                }

                if (TimeElapsed < StopTimeOfText)
                {
                    TextBox.Draw(camera);
                }

                if (CurrentAnimation != null)
                {
                    base.Draw(camera);
                }
            }

        }
    }
}
